<template>
	<el-card>
		<div class="ruleManaClass">
			<el-form :inline="true" :model="state.queryForm">
				<el-form-item>
					<el-input v-model="state.queryForm.ruleName" placeholder="请输入规则名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-select v-model="state.queryForm.ifInvalid" placeholder="请选择状态" style="width: 120px">
						<el-option label="开启" value="0">开启</el-option>
						<el-option label="关闭" value="1">关闭</el-option>
					</el-select>
				</el-form-item>

				<el-form-item>
					<el-button :icon="Search" @click="searchBtn" type="primary">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Refresh" @click="resets">重置</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Plus" v-auth="'patient-primary:rule-mana:add'" type="primary" @click="addOrUpdateHandle()">新增规则</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Delete" v-auth="'patient-primary:rule-mana:delete'" type="danger" @click="deletesHandle">批量删除</el-button>
				</el-form-item>
			</el-form>
			<el-table
				v-loading="state.dataListLoading"
				:data="state.dataList"
				border
				max-height="calc(100vh - 300px )"
				style="width: 100%"
				@selection-change="selectionChangeHandle"
			>
				<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
				<el-table-column type="index" label="序号" width="100" align="center"> </el-table-column>
				<el-table-column prop="ruleName" label="规则名称" header-align="center" align="center"></el-table-column>

				<el-table-column prop="remark" label="备注" header-align="center" align="center"></el-table-column>
				<el-table-column prop="ifInvalid" label="状态" header-align="center" align="center">
					<template #default="scope">
						{{ scope.row.ifInvalid == 0 ? '开启' : '关闭' }}
						<el-switch @change="ifInvalidChange($event, scope.row)" v-model="scope.row.ifInvalid" :active-value="0" :inactive-value="1" />
					</template>
				</el-table-column>

				<el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
				<el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
					<template #default="scope">
						<el-button type="primary" v-auth="'patient-primary:rule-mana:edit'" link @click="addOrUpdateHandle(scope.row)">编辑</el-button>
						<el-button type="primary" v-auth="'patient-primary:rule-mana:config'" link @click="configurationHandle(scope.row)">配置</el-button>
						<el-button type="danger" v-auth="'patient-primary:rule-mana:delete'" link @click="deleteIdHandle(scope.row.id, scope.row.ruleName)"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				:current-page="state.page"
				:page-sizes="state.pageSizes"
				:page-size="state.limit"
				:total="state.total"
				layout="total, sizes, prev, pager, next, jumper"
				@size-change="sizeChangeHandle"
				@current-change="currentChangeHandle"
			>
			</el-pagination>

			<!-- 弹窗, 新增 查看 编辑 -->
			<AddOrUpdate ref="addOrUpdateRef" @close="getDataList"></AddOrUpdate>
			<!--  -->
			<div class="drawerClass" style="height: 100%">
				<VersionMana ref="versionManaRef"></VersionMana>
			</div>
		</div>
	</el-card>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks/indexZSY.ts'
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'

import { ElMessage, ElMessageBox } from 'element-plus/es'
import { Delete, Edit, Search, Plus, CircleClose, Refresh } from '@element-plus/icons-vue'
import AddOrUpdate from './addOrUpdate.vue'
import VersionMana from './version-mana.vue'
import { addRule, deleteRule } from '@/api/patient-primary/rule-mana'
import { Model } from 'echarts'

const state: IHooksOptions = reactive({
	dataListUrl: 'empi/empiruleinfo/page',
	deleteUrl: 'empi/jobExecute',
	queryForm: {
		ruleName: '',
		ifInvalid: ''
	},
	primaryKey: 'id',
	dataListSelections: []
})

const addOrUpdateRef = ref()

const addOrUpdateHandle = data => {
	addOrUpdateRef.value.init(data)
}

const versionManaRef = ref()
const configurationHandle = data => {
	versionManaRef.value.init(data)
}

const searchBtn = () => {
	state.page = 1
	getDataList()
}

const resets = () => {
	state.queryForm = {
		ruleName: '',
		ifInvalid: ''
	}

	state.page = 1
	state.limit = 10

	getDataList()
}

const ifInvalidChange = (e, data) => {
	if (e == 1) {
		ElMessageBox.confirm('不可手动关闭，需开启一条规则，谢谢！', '提示', {
			cancelButtonText: '关闭',
			type: 'warning',
			showConfirmButton: false
		})
			.then(() => {
				data.ifInvalid = 0
			})
			.catch(() => {
				data.ifInvalid = 0
			})
	} else {
		ElMessageBox.confirm('开启后，其他规则自动关闭！请确认', '提示', {
			cancelButtonText: '关闭',
			confirmButtonText: '确认开启',
			type: 'warning'
		})
			.then(() => {
				data.ifInvalid = e

				addRule(data)
					.then(res => {
						ElMessage.success({
							message: '切换成功',
							duration: 500,
							onClose: () => {
								getDataList()
							}
						})
					})
					.catch(() => {
						data.ifInvalid = e ? 0 : 1
					})
			})
			.catch(() => {
				data.ifInvalid = 1
			})
	}
}

// 批量删除
const deletesHandle = () => {
	if (state.dataListSelections?.length === 0) {
		ElMessage.warning('请选择删除记录')
		return
	}

	const selData: any = state.dataList?.filter(item => state.dataListSelections?.includes(item.id))

	let title = ''
	selData?.forEach((item, index) => {
		if (index) {
			title = title + ',' + item.ruleName
		} else {
			title = item.ruleName
		}
	})

	ElMessageBox.confirm(`确认批量删除规则【${title}】吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			deleteRule(state.dataListSelections).then(res => {
				ElMessage.success('删除成功')
				getDataList()
			})
		})
		.catch(() => {})
}

// 删除
const deleteIdHandle = (id, name) => {
	ElMessageBox.confirm(`确认删除规则【${name}】吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			deleteRule([id]).then(res => {
				ElMessage.success('删除成功')
				getDataList()
			})
		})
		.catch(() => {})
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteHandle } = useCrud(state)
</script>

<style lang="scss">
.ruleManaClass {
	height: calc(100vh - 170px);
	position: relative;
	overflow: hidden;
}

.ruleManaClass .drawerClass .el-overlay {
	height: 100%;
	position: absolute !important;
	overflow: hidden;
}
</style>
